package xyz.cerberu.cet.resource.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cerberu.cet.global.annotation.JsonFilter;
import xyz.cerberu.cet.global.domain.ExceptionEncode;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.global.domain.Page;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ResultUtils;
import xyz.cerberu.cet.global.utils.StringUtils;
import xyz.cerberu.cet.resource.domian.Resource;
import xyz.cerberu.cet.resource.service.ResourceServiceImpl;
import xyz.cerberu.cet.subtitle.domain.Subtitle;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author β世界
 * Created on 14:46
 * @Description 资源控制器
 */
@Controller
@RequestMapping("/resource")
public class ResourceControl {
    private final ResourceServiceImpl resourceServiceImpl;
    private final Global global;

    @Autowired
    public ResourceControl(ResourceServiceImpl resourceServiceImpl, Global global) {
        this.resourceServiceImpl = resourceServiceImpl;
        this.global = global;
    }

    /**
     * @author β世界 on 14:48 2020/4/18
     * @Description 获取到最近更新的资源
     * 查询今天往后的3天更新到的资源，每页显示10条
     **/
    @JsonFilter(type = Resource.class,filter = "res_id,ani_id")
    @ResponseBody
    @RequestMapping(value = "/recentResource",method = RequestMethod.GET)
    public Result<Object> recentResource(int page){
        // 检查当前页数是否合理
        if(page <= 0){
            page = 1;
        }
        // 开启分页处理
        List<Resource> resources = resourceServiceImpl.assignTimeResource(LocalDate.now());
        Page<Resource> pages = Page.build(resources,page, global.getPageShowNumber());

        return ResultUtils.genSuccessResult(pages);
    }

    /**
     * @author β世界 on 16:00 2020/7/14
     * 获取一个指定日期的动漫资源todayAnime
     * @param time: 指定的时间
     * @return com.Cet.common.domain.Result
     **/
    @JsonFilter(type = Resource.class,filter = "ani_id,res_id")
    @ResponseBody
    @RequestMapping(value = "/appoint",method = RequestMethod.GET)
    public Result<Object> appoint(String time,int page){
        Result<Object> result;
        if(!StringUtils.dateFormat(time)){
            return ResultUtils.genErrorResult(ExceptionEncode.CODE_EXCEPTION,"时间格式出错");
        }
        LocalDate localDate = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Page<Resource> pages = Page.build(resourceServiceImpl.appointTime(localDate),page, global.getPageShowNumber());
        result = ResultUtils.genSuccessResult(pages);
        return result;
    }

    /**
     * @author β世界 on 14:19 2020/4/21
     * @Description 获取一个动漫的所有相关资源
     **/
    @JsonFilter(type = Resource.class,filter = "res_id,ani_id,subtitle")
    @ResponseBody
    @RequestMapping(value = "/correlationResource",method = RequestMethod.POST)
    public Result<Object> animeCorrelationResource(@Param("ani_id") String ani_id){
        // 将结果返回给前端
        MultiValueMap<Subtitle,Resource> map = resourceServiceImpl.getAnimeFullResource(ani_id);
        if(map.size() == 0){
            return ResultUtils.genFailResult("无相关资源");
        }
        return ResultUtils.genSuccessResult(map);
    }

}
